using System;
using Microsoft.SPOT;
using dotnetwarrior.NetMF.Game;
using Gadgeteer.Modules.GHIElectronics;

namespace Pacman
{
  class AudioManager
  {
    private Music _music;
    private byte[] _eatPillSound;
    private byte[] _eatPowerPillSound;
    private byte[] _eatGhostSound;
    private byte[] _dieSound;

    public AudioManager(Music music)
    {     
      _music = music;
      _music.SetVolume(255);

      _eatPillSound = Resources.GetBytes(Resources.BinaryResources.PelletEat1);
      _eatPowerPillSound = Resources.GetBytes(Resources.BinaryResources.Invincible);
      _eatGhostSound = Resources.GetBytes(Resources.BinaryResources.MonsterEaten);
      _dieSound = Resources.GetBytes(Resources.BinaryResources.PacmanEaten);

      MessageService.Instance.Subscribe(typeof(Messages.StartGameMessage), StartGameMessage);
      MessageService.Instance.Subscribe(typeof(Messages.AtePowerPillMessage), AtePowerPillMessage);
      MessageService.Instance.Subscribe(typeof(Messages.AtePillMessage), AtePillMessage);
      MessageService.Instance.Subscribe(typeof(Messages.PacmanAteGhostMessage), AteGhostMessage);
      MessageService.Instance.Subscribe(typeof(Messages.PacmanDyingMessage), PacmanDyingMessage);
    }

    private void StartGameMessage(object message)    
    {
      if (_music.IsBusy) _music.StopPlaying();
    }

    private void AtePillMessage(object message)
    {
      if (!_music.IsBusy)
      {
        _music.Play(_eatPillSound);
      }
    }

    private void AtePowerPillMessage(object message)
    {
      if (_music.IsBusy) _music.StopPlaying();
      _music.Play(_eatPowerPillSound);      
    }

    private void AteGhostMessage(object message)
    {
      if (_music.IsBusy) _music.StopPlaying();
      _music.Play(_eatGhostSound);      
    }

    private void PacmanDyingMessage(object message)
    {
      if (_music.IsBusy) _music.StopPlaying();
      _music.Play(_dieSound);
    }
  }
}
